package com.winchain.chainbill;

import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.framework.utils.ParameterHelper;
import com.shell.governor.modules.vo.ParameterVo;
import java.text.ParseException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ChainBillUtil {
	private static final Log LOGGER = LogFactory.getLog(ChainBillUtil.class);
	private static String CHAIN_SWITCH_KEY = "chain.switch";
	private static String ESIGN_SWITCH_KEY = "esign.switch";
	private static Boolean CHAIN_SWITCH_ON;
	private static Boolean ESIGN_SWITCH_ON;
	private static String PAID_MODE_KEY = "paid.mode";
	private static Boolean ONLINE_PAY;
	private static String EXP_SETTLE_MODE;
	private static String CFCA_SWITCH_KEY = "cfca.switch";
	private static Boolean CFCA_SWITCH_ON;

	public static String getLimitDate(String limitDateCode) {
		String limitDate = DateUtil.getSystemDate();

		ParameterVo param = ParameterHelper.getCommonParameter(limitDateCode);
		if (param != null) {
			String value = param.getParmValue();
			if (StringUtil.notEmpty(value)) {
				try {
					limitDate = DateUtil.getPreDay(
							DateUtil.parseDate(DateUtil.getSystemDateTime(), "yyyy-MM-dd HH:mm:ss", "yyyyMMdd"),
							Integer.valueOf(value).intValue());
				} catch (NumberFormatException e) {
					e.printStackTrace();
					LOGGER.error("��������������������", e);
				} catch (ParseException e) {
					LOGGER.error("��������������������", e);
				}
			}
		}
		return limitDate;
	}

	public static boolean isTimeout(String limitDate) {
		boolean isTimeout = false;
		String curDate = DateUtil.getSystemDate();
		int val = DateUtil.compare(limitDate, curDate);
		if (val < 0) {
			isTimeout = true;
		}
		return isTimeout;
	}

	public static boolean onChain() {
		if (CHAIN_SWITCH_ON == null) {
			CHAIN_SWITCH_ON = Boolean.valueOf(false);
			String chainSwitch = SpringContextUtil.getProperty(CHAIN_SWITCH_KEY);
			if ((StringUtil.notEmpty(chainSwitch)) && ("on".equals(chainSwitch))) {
				CHAIN_SWITCH_ON = Boolean.valueOf(true);
			}
		}
		return CHAIN_SWITCH_ON.booleanValue();
	}

	public static boolean onlinePay() {
		if (ONLINE_PAY == null) {
			ONLINE_PAY = Boolean.valueOf(false);
			String paidMode = SpringContextUtil.getProperty(PAID_MODE_KEY);
			if ((StringUtil.notEmpty(paidMode)) && ("online".equals(paidMode))) {
				ONLINE_PAY = Boolean.valueOf(true);
			}
		}
		return ONLINE_PAY.booleanValue();
	}

	public static String getExpSettleMode(String grpId) {
		EXP_SETTLE_MODE = "single";
		if (!StringUtil.emptyAndNull(grpId)) {
			ParameterVo parameter = ParameterHelper.getParameter("6", "EXP_SETTLE_MODE", grpId);
			if (parameter != null) {
				EXP_SETTLE_MODE = parameter.getParmValue();
			}
		}
		return EXP_SETTLE_MODE;
	}

	public static boolean onEsign() {
		if (ESIGN_SWITCH_ON == null) {
			ESIGN_SWITCH_ON = Boolean.valueOf(false);
			String esignSwitch = SpringContextUtil.getProperty(ESIGN_SWITCH_KEY);
			if ((StringUtil.notEmpty(esignSwitch)) && ("on".equals(esignSwitch))) {
				ESIGN_SWITCH_ON = Boolean.valueOf(true);
			}
		}
		return ESIGN_SWITCH_ON.booleanValue();
	}

	public static boolean signContOnline() {
		return (onEsign()) || (onCfca());
	}

	public static boolean onCfca() {
		if (CFCA_SWITCH_ON == null) {
			CFCA_SWITCH_ON = Boolean.valueOf(false);
			String esignSwitch = SpringContextUtil.getProperty(CFCA_SWITCH_KEY);
			if ((StringUtil.notEmpty(esignSwitch)) && ("on".equals(esignSwitch))) {
				CFCA_SWITCH_ON = Boolean.valueOf(true);
			}
		}
		return CFCA_SWITCH_ON.booleanValue();
	}
}
